//
//  ViewController.m
//  谈诗说词
//
//  Created by dllo on 15/6/11.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "HeadView.h"
#import "ListCell.h"
#import "PomeDetailController.h"
#import "PoemDatabase.h"
#import "ChanceController.h"
#import "SearchController.h"


#import "LeftViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController
- (void)dealloc
{
    
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    //self.array = [NSMutableArray array];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]];
    self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0.32 * WIDTH, 0.08 * WIDTH)];
    title.text = @"谈诗说词";
    title.font = [UIFont fontWithName:@"-" size:28];
    self.navigationItem.titleView = title;
    
    
    
    // 右按钮
    UIImage * shaixuan = [UIImage imageNamed:@"筛选.png"];
    // 图片原始颜色
    shaixuan = [shaixuan imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * chance = [[UIBarButtonItem alloc] initWithImage:shaixuan style:UIBarButtonItemStylePlain target:self action:@selector(chanceAction:)];
    [self.navigationItem setRightBarButtonItem:chance];
    
    UIImage *a = [UIImage imageNamed:@"抽屉开关.png"];
    a = [a imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:a style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    [self.navigationItem setLeftBarButtonItem:left];

    
    
    
//    
//    NSArray *familyNames = [UIFont familyNames];
//    
//    for( NSString *familyName in familyNames ){
//        
//        NSLog( @"Family: %s \n", [familyName UTF8String] );
//        
//        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
//        
//        for( NSString *fontName in fontNames ){
//            
//            NSLog( @"\tFont: %s \n", [fontName UTF8String] );
//            
//        }
//        
//    }
    
    self.array = [NSMutableArray arrayWithArray:[PoemDatabase selectAll]];
    [self createTable];
    
}

- (void)createTable
{
    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [background setImage:[UIImage imageNamed:@"背景.png"]];
    [self.view  addSubview:background];
    
    
    UIButton *search = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.08 * WIDTH)];
    [search setImage:[UIImage imageNamed:@"搜索.png"] forState:UIControlStateNormal];
    
    
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
   // table.rowHeight = 120;
    HeadView*head = [[HeadView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 0.54 * WIDTH)];
    
    self.number = arc4random()%400;
    PoemModel *model = [self.array objectAtIndex:self.number - 1];
    NSArray *arr = [model.content componentsSeparatedByString:@"。"];
    head.contentLabel.text = [NSString stringWithFormat:@"%@。", [arr firstObject]];
    head.titlabel.text = model.name;
    head.authorLabel.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    head.target = self;
    head.action = @selector(topViewAction:);
    [head.search addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
    table.tableHeaderView = head;
    table.delegate = self;
    table.dataSource = self;
    [table setBackgroundColor:[UIColor clearColor]];
    table.alpha = 0.7;
    [self.view addSubview:table];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    PoemModel *model = [self.array objectAtIndex:self.number + indexPath.row];
    cell.title.text = model.name;
    NSArray *array = [model.content componentsSeparatedByString:@"。"];
    NSString *content = [NSString stringWithFormat:@"%@。", [array firstObject]];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"X-" size:19]};
    self.rect = [content boundingRectWithSize:CGSizeMake(WIDTH - 0.14 * WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.pome.frame = CGRectMake(0.07 * WIDTH, 0.22 * WIDTH, 0.86 * WIDTH, self.rect.size.height);
    cell.pome.text = content;
    
    cell.author.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    
    return cell;
}
#pragma mark - cell自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rect.size.height + 0.26 * WIDTH;
}

#pragma mark - cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PoemModel *model = [self.array objectAtIndex:indexPath.row + self.number];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
    detail.pomeTitle = model.name;
    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
     [self.navigationController.view.layer addAnimation:trans forKey:nil];
}
- (void)topViewAction:(id)sender
{
    PoemModel *model = [self.array objectAtIndex:self.number - 1];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
    detail.pomeTitle = model.name;
    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];

}

- (void)chanceAction:(id)sender
{
    ChanceController *chance = [[ChanceController alloc]init];
    [self.navigationController pushViewController:chance animated:YES];
}


#pragma mark - 搜索
- (void)searchAction
{
    SearchController *search = [[SearchController alloc]init];
    [self.navigationController pushViewController:search animated:YES];

}
- (void)leftAction:(id)sender
{
     //抽屉效果的左视图控制器
//            LeftViewController *left = [[LeftViewController alloc]init];
//            YRSideViewController *side = [[YRSideViewController alloc]initWithNibName:nil bundle:nil];
//            // tabBar设为抽屉的中间(主要)控制器
//            side.rootViewController = self;
//            side.leftViewController = left;
//            side.leftViewShowWidth = 120;
//    [self.navigationController pushViewController:left animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showLeft" object:@(YES)];
    
};


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
